﻿/********************************************************************
	created:	2011/05/29
	created:	29:5:2011   21:17
	filename: 	D:\My Documents\Visual Studio 2010\Projects\IntranetMessagerClient\Client\LoginWindow.cs
	file path:	D:\My Documents\Visual Studio 2010\Projects\IntranetMessagerClient\Client
	file base:	LoginWindow
	file ext:	cs
	author:		
	
	purpose:	登录窗口功能实现
*********************************************************************/
using System;
using System.Windows.Forms;
using CommonLibrary;

namespace Client
{
    public partial class LoginWindow : Form
    {
        public LoginWindow()
        {
            InitializeComponent();
        }

        private void ExitBtnClick(object sender, EventArgs e)
        {
            MainWindow.MainWindowThisUser.IsLogin = false;
            MainWindow.MainWindowThisUser = null;
            Close();
        }

        private static void RegLinkLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MainWindow.SyncContext.Post(reg =>
                                            {
                                                MainWindow.ThisRegWindow = new RegWindow();
                                                MainWindow.ThisRegWindow.Show();
                                            }
                                        , null);
        }

        private void LoginBtnClick(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(loginIdText.Text))
            {
                return;
            }
            var userMsg = new Protocol.SignIn
                              {
                                  MyUid = Convert.ToInt32(loginIdText.Text),
                                  MyPassword = textBoxPassword.Text
                              };

            byte[] messageSend = CommonFunctionClass.SealMessageToEnvelope(userMsg, userMsg.MyUid, false, true);
            MainWindow.SendDataDelegateFunc.BeginInvoke(messageSend, null, null);
            Close();
        }

        private void TextBox1KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Enter:
                    LoginBtnClick(sender, e);
                    break;
                case Keys.Escape:
                    ExitBtnClick(sender, e);
                    break;
            }
        }
    }
}